﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBOffice.Models;
using System.Data;
using System.Data.SqlClient;

namespace PBOffice.DAL
{
    public class RoleInfoService
    {
        public List<RoleInfo> GetAllRoleInfo()
        {
            List<RoleInfo> list = new List<RoleInfo>();
            string sql = "select * from RoleInfo";
            using (IDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {
                    RoleInfo role = new RoleInfo();
                    role.RoleId = Convert.ToInt32(dr["RoleId"]);
                    role.RoleName = dr["RoleName"].ToString();
                    role.RoleDesc = dr["RoleDesc"].ToString();
                    list.Add(role);
                }
            }
            return list;
        }
        public void DelteRoleInfo(int id)
        {
            string sql = string.Format("delete RoleInfo where RoleId={0}", id);
            SqlHelp.ExecuteNonQuery(SqlHelp.connectionstring, CommandType.Text, sql);
        }
        public void AddRoleInfo(RoleInfo role)
        {
            string sql = "insert [RoleInfo] (RoleName,RoleDesc) values (@RoleName,@RoleDesc)";
            SqlParameter[] spa = {
                                     new SqlParameter("@RoleName",role.RoleName),
                                     new SqlParameter("@RoleDesc",role.RoleDesc)
                                 };
            SqlHelp.ExecuteNonQuery(SqlHelp.connectionstring, CommandType.Text, sql, spa);
        }
        public void UpdateRoleInfo(RoleInfo role)
        {
            string sql = "update [RoleInfo] set RoleName=@RoleName,RoleDesc=@RoleDesc where RoleId=@RoleId";
            SqlParameter[] spa = {
                                     new SqlParameter("@RoleId",role.RoleId),
                                     new SqlParameter("@RoleName",role.RoleName),
                                     new SqlParameter("@RoleDesc",role.RoleDesc)
                                 };
            SqlHelp.ExecuteNonQuery(SqlHelp.connectionstring, CommandType.Text, sql, spa);
        }
        public RoleInfo GetRoleInfoById(int id)
        {
            RoleInfo role = new RoleInfo();
            string sql = string.Format("select * from RoleInfo where RoleId={0}", id);
            using (IDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {

                    role.RoleId = Convert.ToInt32(dr["RoleId"]);
                    role.RoleName = dr["RoleName"].ToString();
                    role.RoleDesc = dr["RoleDesc"].ToString();

                }
            }
            return role;
        }
    }
}
